from django.shortcuts import render, HttpResponse
from django.views.decorators.http import require_http_methods, require_GET, require_POST
from .models import Article


# TODO: 限制为只能通过GET方式请求
@require_GET
def index(request):
    articles = Article.objects.all()
    context = {
        'articles': articles
    }
    return render(request, 'index.html', context=context)


@require_http_methods(['GET', 'POST'])
def article(request):
    if request.method == 'GET':
        return render(request, 'article.html')
    else:
        title = request.POST.get('title')
        content = request.POST.get('content')
        price = request.POST.get('price')
        Article.objects.create(title=title, content=content, price=price)
        return HttpResponse('success')
